package com.enzoism.thread.a01threadthreadpoolexecuter;

import java.time.Instant;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * User: enzoism
 * Date: 2020/8/6- 23:41
 */
public class A02_ThreadPoolExecutor {

    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final int QUEUE_CAPACITY = 100;
    private static final Long KEEP_ALIVE_TIME = 1L;

    public static void main(String[] args) {

        BlockingQueue<Runnable> myQueue = new ArrayBlockingQueue<>(QUEUE_CAPACITY);
        MyTreadFactory myTreadFactory = new MyTreadFactory();
        MyRejectPolicy myRejectPolicy = new MyRejectPolicy();

        //使用阿里巴巴推荐的创建线程池的方式
        //通过ThreadPoolExecutor构造函数自定义参数创建
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.SECONDS,
                myQueue,
                myTreadFactory,
                myRejectPolicy
                );
        for (int i = 1; i <= 10; i++) {
            Test01ThreadRunnable task = new Test01ThreadRunnable();
            executor.execute(task);
        }

        //终止线程池
        executor.shutdown();
        try {
            executor.awaitTermination(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Finished all threads");
    }

    // 创建一个Runnable执行任务
    static class Test01ThreadRunnable implements Runnable {
        private int count = 5;
        @Override
        public void run() {
            for (int i = 0; i < 7; i++) {
                if (count > 0) {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                        System.out.println("CurrentThread name:" + Thread.currentThread().getName() + "  date：" + Instant.now());
                        System.out.println(Thread.currentThread().getName() + "----currentIndex:" + (count--));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    // 创建一个自定义的线程工程类，定义线程的名称和常规属性
    static class MyTreadFactory implements ThreadFactory {
        private final AtomicInteger mThreadNum = new AtomicInteger(1);
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r, "my-thread-" + mThreadNum.getAndIncrement());
            System.out.println(t.getName() + " has been created");
            return t;
        }
    }

    public static class MyRejectPolicy implements RejectedExecutionHandler {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            doLog(r, e);
        }
        // 可做日志记录等
        private void doLog(Runnable r, ThreadPoolExecutor e) {
            System.err.println( r.toString() + " rejected"+"  ----->completedTaskCount: " + e.getCompletedTaskCount());
        }
    }
}
